/**
 * 加法函数，用来得到精确的加法结果
 * @param left_operand
 * @param right_operand
 * @param scale
 * @returns {string}
 */
export const bcadd = (left_operand, right_operand, scale = 2) => {
  const r1 = deal(left_operand)
  const r2 = deal(right_operand)
  const m = Math.pow(10, Math.max(r1, r2))
  return ((left_operand * m + right_operand * m) / m).toFixed(scale)
}

/**
 * 减法函数，用来得到精确的减法结果
 * @param left_operand
 * @param right_operand
 * @param scale
 * @returns {string}
 */
export const bcsub = (left_operand, right_operand, scale = 2) => {
  const r1 = deal(left_operand)
  const r2 = deal(right_operand)
  const m = Math.pow(10, Math.max(r1, r2))
  return ((left_operand * m - right_operand * m) / m).toFixed(scale)
}

/**
 * 乘法函数，用来得到精确的乘法结果
 * @param left_operand
 * @param right_operand
 * @param scale
 * @returns {string}
 */
export const bcmul = (left_operand, right_operand, scale = 2) => {
  let m = 0
  m += deal(left_operand)
  m += deal(right_operand)
  const r1 = Number(left_operand.toString().replace('.', ''))
  const r2 = Number(right_operand.toString().replace('.', ''))
  return ((r1 * r2) / Math.pow(10, m)).toFixed(scale)
}

/**
 * 除法函数，用来得到精确的除法结果
 * @param left_operand
 * @param right_operand
 * @param scale
 * @returns {string}
 */
export const bcdiv = (left_operand, right_operand, scale = 2) => {
  const t1 = deal(left_operand)
  const t2 = deal(right_operand)
  const r1 = Number(left_operand.toString().replace('.', ''))
  const r2 = Number(right_operand.toString().replace('.', ''))
  return ((r1 / r2) * Math.pow(10, t2 - t1)).toFixed(scale)
}

/**
 * 求小数点后的数据长度
 * @param arg
 * @returns {number}
 */
function deal(arg) {
  let t = 0
  try {
    t = arg.toString().split('.')[1].length
    // eslint-disable-next-line no-empty
  } catch (e) { }
  return t
}
